package cn.stylefeng.guns.modular.make.service.impl;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicCustomerParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicOperateParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCustomerResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicOperateResult;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.make.entity.MakeRailwayPlatformManage;
import cn.stylefeng.guns.modular.make.mapper.MakeRailwayPlatformManageMapper;
import cn.stylefeng.guns.modular.make.model.params.MakeRailwayPlatformManageParam;
import cn.stylefeng.guns.modular.make.model.params.MakeRailwayPlatformParam;
import cn.stylefeng.guns.modular.make.model.result.MakeRailwayPlatformManageResult;
import cn.stylefeng.guns.modular.make.model.result.MakeRailwayPlatformResult;
import  cn.stylefeng.guns.modular.make.service.MakeRailwayPlatformManageService;
import cn.stylefeng.guns.modular.make.service.MakeRailwayPlatformService;
import cn.stylefeng.guns.modular.task.model.params.TaskOrderParam;
import cn.stylefeng.guns.modular.task.model.result.TaskOrderResult;
import cn.stylefeng.guns.modular.task.service.TaskOrderService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 月台预约管理 服务实现类
 */
@Service
public class MakeRailwayPlatformManageServiceImpl extends ServiceImpl<MakeRailwayPlatformManageMapper, MakeRailwayPlatformManage> implements MakeRailwayPlatformManageService {

    @Override
    public void add(MakeRailwayPlatformManageParam param){
        MakeRailwayPlatformManage entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MakeRailwayPlatformManageParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(MakeRailwayPlatformManageParam param){
        MakeRailwayPlatformManage oldEntity = getOldEntity(param);
        MakeRailwayPlatformManage newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MakeRailwayPlatformManageResult findBySpec(MakeRailwayPlatformManageParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<MakeRailwayPlatformManageResult> findListBySpec(MakeRailwayPlatformManageParam param){
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(MakeRailwayPlatformManageParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition,operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

	@Override
	public LayuiPageInfo findMyPageBySpec(MakeRailwayPlatformManageParam param, String condition,Long operateId) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findMyPageBySpec(pageContext, param,condition,operateId);
        return LayuiPageFactory.createPageInfo(page);
	}

    private Serializable getKey(MakeRailwayPlatformManageParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MakeRailwayPlatformManage getOldEntity(MakeRailwayPlatformManageParam param) {
        return this.getById(getKey(param));
    }

    private MakeRailwayPlatformManage getEntity(MakeRailwayPlatformManageParam param) {
        MakeRailwayPlatformManage entity = new MakeRailwayPlatformManage();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public MakeRailwayPlatformManageResult checkBySpec(MakeRailwayPlatformManageParam param) {
		return this.baseMapper.checkBySpec(param);
	}

	@Override
	public Integer addReturnId(MakeRailwayPlatformManageParam param) {
		return this.baseMapper.addReturnId(param);
	}

    @Resource
    private TaskOrderService taskOrderService;

    @Resource
    private BasicCustomerService basicCustomerService;

    @Resource
    private BasicOperateService basicOperateService;

    @Resource
    private MakeRailwayPlatformService makeRailwayPlatformService;

	@Override
	public ResponseData addMake(MakeRailwayPlatformManageParam param, User user,BasicOperate operate) {
    	MakeRailwayPlatformParam makeRailwayPlatformParam = new MakeRailwayPlatformParam();
    	makeRailwayPlatformParam.setDeptId(operate.getDeptId());
    	makeRailwayPlatformParam.setRailwayPlatformName(param.getRailwayPlatformName());
    	MakeRailwayPlatformResult makeRailwayPlatformResult = makeRailwayPlatformService.findBySpec(makeRailwayPlatformParam,Long.parseLong(operate.getId().toString()));
    	
    	MakeRailwayPlatformManageParam maketo = new MakeRailwayPlatformManageParam();
    	maketo.setArriveTime(param.getArriveTime().substring(0, 10));
    	operate.setDeptId(user.getDeptId());
    	int countmake = this.countBydate(maketo,Long.parseLong(operate.getId().toString()))+1;
    	String v2number = countmake+"";
    	if(countmake<10) {
    		v2number = "0"+v2number;
    	}
    	param.setV2(param.getArriveTime().substring(0, 10).replaceAll("-", "")+(v2number));
    	
    	param.setState("已预约");
    	param.setRailwayPlatformId(Long.parseLong(makeRailwayPlatformResult.getId().toString()));
    	param.setDeptId(operate.getDeptId());
    	param.setDeliverId(operate.getDeliverId());
    	param.setCreateTime(new Date());
    	param.setCreateUserName(user.getName());
    	
    	MakeRailwayPlatformManageParam checkparam = new MakeRailwayPlatformManageParam();
    	checkparam.setRailwayPlatformId(param.getRailwayPlatformId());
    	checkparam.setArriveTime(param.getArriveTime());
    	checkparam.setLeaveTime(param.getLeaveTime());
    	MakeRailwayPlatformManageResult makeRailwayPlatformManageResult = this.checkBySpec(checkparam);
    	if(makeRailwayPlatformManageResult != null) {
    		return ResponseData.error("该时间段已预约，请错开时间");
    	}
    	this.addReturnId(param);

    	//新增作业单
    	TaskOrderParam taskOrderParam = new TaskOrderParam();
    	
    	taskOrderParam.setStatus("未开始");
    	
    	TaskOrderParam to = new TaskOrderParam();
    	to.setV1(DateUtil.getDate());
    	to.setDeptId(operate.getDeptId());
    	to.setDeliverId(operate.getDeliverId());
    	int count = taskOrderService.countBydate(to,user.getOperateId());
    	DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");//设置日期格式
    	taskOrderParam.setOrderNumber(user.getAccount().substring(0, 3)+LocalDateTime.now().format(fmt)+(count+1));
    	
    	taskOrderParam.setCustomerNumber(param.getCustomerOrder());
    	
    	BasicCustomerParam basicCustomerParam = new BasicCustomerParam();
    	basicCustomerParam.setDeptId(operate.getDeptId());
    	BasicCustomerResult basicCustomerResult = basicCustomerService.findBySpec(basicCustomerParam,Long.parseLong(operate.getId().toString()));
    	if(basicCustomerResult != null) {
    		taskOrderParam.setOutCustomerId(Long.parseLong(basicCustomerResult.getId().toString()));
    		taskOrderParam.setOutCustomerName(basicCustomerResult.getCustomerName());
    	}
    	
		taskOrderParam.setOperateId(operate.getId());
		taskOrderParam.setOperateName(operate.getOperateName());
    	taskOrderParam.setName(param.getCarNumber()+param.getMakeType()=="提货预约"?"装车":"卸车");
    	taskOrderParam.setTaskWork(param.getMakeType()=="提货预约"?"装车":"卸车");
    	taskOrderParam.setSettlementMode("体积");
    	taskOrderParam.setVolume(param.getVolume());
    	taskOrderParam.setPeoples("0");
    	taskOrderParam.setDeptId(operate.getDeptId());
    	taskOrderParam.setDeliverId(operate.getDeliverId());
    	taskOrderParam.setV1(param.getArriveTime().substring(0, 10));
    	taskOrderParam.setV2(DateUtil.getDateS());
    	taskOrderParam.setV3(user.getName());
    	taskOrderParam.setV6("0");
    	taskOrderParam.setCarModel(param.getCarType());
    	taskOrderParam.setV7(user.getUserId()+"");
    	taskOrderParam.setMakeId(Long.parseLong(param.getId().toString()));
    	taskOrderService.add(taskOrderParam);
		return ResponseData.success();
	}

	@Override
	public ResponseData editMake(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam,User user) {
    	MakeRailwayPlatformParam makeRailwayPlatformParam = new MakeRailwayPlatformParam();
    	makeRailwayPlatformParam.setDeptId(user.getDeptId());
    	makeRailwayPlatformParam.setRailwayPlatformName(makeRailwayPlatformManageParam.getRailwayPlatformName());
    	MakeRailwayPlatformResult makeRailwayPlatformResult = makeRailwayPlatformService.findBySpec(makeRailwayPlatformParam,user.getOperateId());
    	makeRailwayPlatformManageParam.setRailwayPlatformId(Long.parseLong(makeRailwayPlatformResult.getId().toString()));
    	
    	MakeRailwayPlatformManageParam checkparam = new MakeRailwayPlatformManageParam();
    	checkparam.setId(makeRailwayPlatformManageParam.getId());
    	checkparam.setRailwayPlatformId(makeRailwayPlatformManageParam.getRailwayPlatformId());
    	checkparam.setArriveTime(makeRailwayPlatformManageParam.getArriveTime());
    	checkparam.setLeaveTime(makeRailwayPlatformManageParam.getLeaveTime());
    	MakeRailwayPlatformManageResult makeRailwayPlatformManageResult = this.checkBySpec(checkparam);
    	if(makeRailwayPlatformManageResult != null) {
    		return ResponseData.error("该时间段已预约，请错开时间");
    	}
    	if(makeRailwayPlatformManageParam.getState().equals("已修改")) {
    		this.update(makeRailwayPlatformManageParam);
    		
    		TaskOrderParam taskOrderParam = new TaskOrderParam();
    		taskOrderParam.setMakeId(Long.parseLong(makeRailwayPlatformManageParam.getId().toString()));
    		TaskOrderResult orderResult = taskOrderService.findBySpec(taskOrderParam);
    		
    		taskOrderParam.setName(makeRailwayPlatformManageParam.getCarNumber()+makeRailwayPlatformManageParam.getMakeType()=="提货预约"?"装车":"卸车");
    		taskOrderParam.setTaskWork(makeRailwayPlatformManageParam.getMakeType()=="提货预约"?"装车":"卸车");
    		taskOrderParam.setV1(makeRailwayPlatformManageParam.getArriveTime().substring(0, 10));
    		taskOrderParam.setCarModel(makeRailwayPlatformManageParam.getCarType());
    		taskOrderParam.setId(orderResult.getId());
    		taskOrderService.update(taskOrderParam);
    	}
    	if(makeRailwayPlatformManageParam.getState().equals("已拒绝")) {
    		this.update(makeRailwayPlatformManageParam);
    		
    		TaskOrderParam taskOrderParam = new TaskOrderParam();
    		taskOrderParam.setMakeId(Long.parseLong(makeRailwayPlatformManageParam.getId().toString()));
    		TaskOrderResult orderResult = taskOrderService.findBySpec(taskOrderParam);
    		
    		taskOrderParam.setId(orderResult.getId());
    		taskOrderService.delete(taskOrderParam);
    	}
        return ResponseData.success();
	}

	@Override
	public int countBydate(MakeRailwayPlatformManageParam param,Long operateId) {
		Integer a;
		a = this.baseMapper.countBydate(param,operateId);
		if (a == null) {
			return 0;
		}
		return a;
	}

	@Override
	public MakeRailwayPlatformManageResult findJingxingzhongBySpec(MakeRailwayPlatformManageParam param) {
		return this.baseMapper.findJingxingzhongBySpec(param);
	}

	@Override
	public MakeRailwayPlatformManageResult findPaiduizhongBySpec(MakeRailwayPlatformManageParam param) {
		return this.baseMapper.findPaiduizhongBySpec(param);
	}

}
